/*----------------------------------------------------------------------------
 *      RL-ARM - SPI
 *----------------------------------------------------------------------------
 *      Name:    RTX_SPI.h
 *      Purpose: Header for SPI Generic Layer Driver
 *      Rev.:    V4.70
 *----------------------------------------------------------------------------
 *      This code is part of the RealView Run-Time Library.
 *      Copyright (c) 2004-2014 KEIL - An ARM Company. All rights reserved.
 *---------------------------------------------------------------------------*/

#include "RTL.h"      /* RTX kernel functions & defines      */

#ifndef __RTX_SPI_H
#define __RTX_SPI_H

#ifdef __cplusplus
extern "C" {
#endif

// <o0> Number of transmit objects for controllers <1-1024>
//      <i> Determines the size of software message buffer for transmitting.
//      <i> Default: 20
// <o1> Number of receive objects for controllers <1-1024>
//      <i> Determines the size of software message buffer for receiving.
//      <i> Default: 20
#define SPI_TX_BUF_MAX 20
#define SPI_RX_BUF_MAX 20

/* SPI message object structure */
typedef struct
{
    U8 data; /* Data field */
} SPI_msg_t;

/* Externaly declared memory pool for SPI messages, both transmit and receive*/
extern U32 SPI_msg_pool[((sizeof(SPI_msg_t) + 3) / 4) * ((SPI_TX_BUF_MAX + SPI_RX_BUF_MAX)) + 3];

/* Externaly declared mailbox, for SPI transmit messages                     */
extern U32 SPI_MBX_tx_ctrl[4 + SPI_TX_BUF_MAX];

/* Externaly declared mailbox, for SPI receive messages                      */
extern U32 SPI_MBX_rx_ctrl[4 + SPI_RX_BUF_MAX];

/* Semaphores used for protecting writing to SPI hardware                    */
extern OS_SEM SPI_wr_sem;

/* Error values that functions can return */
typedef enum
{
    SPI_OK = 0,                /* No error                              */
    SPI_NOT_IMPLEMENTED_ERROR, /* Function has not been implemented     */
    SPI_MEM_POOL_INIT_ERROR,   /* Memory pool initialization error      */
    SPI_BAUDRATE_ERROR,        /* Baudrate was not set                  */
    SPI_TX_BUSY_ERROR,         /* Transmitting hardware busy            */
    SPI_OBJECTS_FULL_ERROR,    /* No more rx or tx objects available    */
    SPI_ALLOC_MEM_ERROR,       /* Unable to allocate memory from pool   */
    SPI_DEALLOC_MEM_ERROR,     /* Unable to deallocate memory           */
    SPI_TIMEOUT_ERROR,         /* Timeout expired                       */
    SPI_UNEXIST_CTRL_ERROR,    /* Controller does not exist             */
    SPI_UNEXIST_CH_ERROR,      /* Channel does not exist                */
} SPI_ERROR;

/* Functions defined SPI hardware driver (module SPI_chip.c)                 */
extern SPI_ERROR SPI_hw_shutdown();
extern SPI_ERROR SPI_hw_setup();
extern SPI_ERROR SPI_hw_init();
extern SPI_ERROR SPI_hw_start();
extern SPI_ERROR SPI_hw_tx_empty();
extern SPI_ERROR SPI_hw_wr(SPI_msg_t* msg);
extern void SPI3_IRQHandler();

/* Functions defined in module RTX_SPI.c                                     */
SPI_ERROR SPI_init();
SPI_ERROR SPI_start();
SPI_ERROR SPI_send(SPI_msg_t* msg, U16 timeout);
SPI_ERROR SPI_receive(SPI_msg_t* msg, U16 timeout);



#ifdef __cplusplus
}
#endif

#endif /* __RTX_SPI_H */

/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/
